package com.sensePlusVR.utils;

import android.annotation.SuppressLint;

import java.lang.reflect.Method;
import java.util.HashMap;

public class SystemProperty {
	private static final HashMap<String, String> sSystemPropertyMap = new HashMap<String, String>();
	private static final String [] Property ={"ro.chipname","ro.board.platform", "ro.hardware", "ro.boot.hardware","ro.arch","ro.hardware.alter"};
    
	@SuppressLint("DefaultLocale")
	public static String getCpuName(){
		String cpuinfo = null;
		for (String s:Property){
			String info = getSystemPropertyCached(s);
			if (info != null && !info.equals("none")){
				String lowerInfo = info;
				if (info.startsWith("MSM")){
					lowerInfo = info.toLowerCase();
				}
				if (cpuinfo == null){
					cpuinfo = lowerInfo;
				}else{
					cpuinfo = cpuinfo + "," + lowerInfo;
				}
			}
		}
		return cpuinfo;
	}
	static String getSystemPropertyCached(String key) {
        String prop = sSystemPropertyMap.get(key);
        if (prop == null) {
            prop = getSystemProperty(key, "none");
            sSystemPropertyMap.put(key, prop);
        }
        return prop;
    }

    private static String getSystemProperty(String key, String def) {
        try {
            final ClassLoader cl = ClassLoader.getSystemClassLoader();
            final Class<?> SystemProperties = cl.loadClass("android.os.SystemProperties");
            final Class<?>[] paramTypes = new Class[] { String.class, String.class };
            final Method get = SystemProperties.getMethod("get", paramTypes);
            final Object[] params = new Object[] { key, def };
            return (String) get.invoke(SystemProperties, params);
        } catch (Exception e){
            return def;
        }
    }
}
